package cn.new7as.cas5s.ui.apply;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;

import com.hjq.http.EasyHttp;
import com.hjq.http.lifecycle.ActivityLifecycle;
import com.hjq.http.listener.OnHttpListener;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;

import java.util.List;

import cn.new7as.cas5s.databinding.ActivityApplyProductBinding;
import cn.new7as.cas5s.ui.slideshow.BaseActivity;
import cn.new7as.cas5s.ui.easy.netapi.HttpData;
import cn.new7as.cas5s.ui.easy.netapi.ProductCateChildApi;


public class ApplyActivity extends BaseActivity {


    private ActivityApplyProductBinding binding;

    @SuppressLint("MissingPermission")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityApplyProductBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        setop("Apply");

        initV();


    }

    List<ProductCateChildApi.ProductCateChildData> results;

    @Override
    public void initV() {
        super.initV();
        show();
        String data = getIntent().getStringExtra("data");
        binding.availableloanamount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!results.isEmpty()) {
                    String[] strings = new String[results.size()];
                    for (int i = 0; i < results.size(); i++) {
                        strings[i] = results.get(i).loanAmount.replace("₹","");
                    }

                    new XPopup.Builder(ApplyActivity.this)
                            .isDestroyOnDismiss(true)

                            .asBottomList("Available Loan Amount (₹)", strings,
                                    null, -1,
                                    new OnSelectListener() {
                                        @Override
                                        public void onSelect(int position, String text) {
                                            sta=position;
                                            binding.availableloanamount.setText(results.get(position).loanAmount.replace("₹", ""));
                                            binding.loanperiod.setText(results.get(position).periodLength + "");
                                            binding.interate.setText(results.get(position).interestRate);
                                            binding.servicetate.setText(results.get(position).processingFeeRate);
                                        }
                                    })
                            .show();
                }
            }
        });
        binding.loanperiod.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!results.isEmpty()) {
                    String[] strings = new String[results.size()];
                    for (int i = 0; i < results.size(); i++) {
                        strings[i] = results.get(i).periodLength + "";
                    }

                    new XPopup.Builder(ApplyActivity.this)
                            .isDestroyOnDismiss(true)

                            .asBottomList("Loan Period (days)", strings,
                                    null, -1,
                                    new OnSelectListener() {
                                        @Override
                                        public void onSelect(int position, String text) {
                                            sta=position;
                                            binding.availableloanamount.setText(results.get(position).loanAmount.replace("₹", ""));
                                            binding.loanperiod.setText(results.get(position).periodLength + "");
                                            binding.interate.setText(results.get(position).interestRate);
                                            binding.servicetate.setText(results.get(position).processingFeeRate);
                                        }
                                    })
                            .show();
                }
            }
        });
        EasyHttp.get(new ActivityLifecycle(this))
                .api(new ProductCateChildApi(getunique(), data))
                .request(new OnHttpListener<HttpData<List<ProductCateChildApi.ProductCateChildData>>>() {
                    @Override
                    public void onHttpSuccess(@NonNull HttpData<List<ProductCateChildApi.ProductCateChildData>> result) {
                        results = result.getResult();
                        sta=0;
                        binding.availableloanamount.setText(results.get(sta).loanAmount.replace("₹", ""));
                        binding.loanperiod.setText(results.get(sta).periodLength + "");
                        binding.interate.setText(results.get(sta).interestRate);
                        binding.servicetate.setText(results.get(sta).processingFeeRate);
                        hide();
                    }

                    @Override
                    public void onHttpFail(@NonNull Throwable throwable) {

                    }
                });


        binding.verifyme.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sta==-1)return;
               Intent intent=new Intent(ApplyActivity.this, ApplyConfirmActivity.class);
               Bundle bundle=new Bundle();
                bundle.putSerializable("ddba",results.get(sta));
                intent.putExtras(bundle);
               startActivity(intent);
               finish();
            }
        });
    }


    int sta = -1;


}